# -*- coding = utf-8 -*-
# @Time : 2023/5/19 13:18
# @Author :Henry L
# @File : GD.py
import glob
import os
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait


class SD:
    def __init__(self):
        options = webdriver.ChromeOptions()
        self.host = "https://www.gdmede.com.cn/"
        prefs = {"plugins.always_open_pdf_externally": True,
                 "download.default_directory": os.getcwd() + "\\"}
        options.add_experimental_option("prefs", prefs)
        self.url = 'https://www.gdmede.com.cn/'
        self.driver = webdriver.Chrome(options=options)
        self.driver.maximize_window()
        self.wait = WebDriverWait(driver=self.driver, timeout=10)
        self.driver.get(self.url)

    def get(self, page):
        time.sleep(3)
        # tbody = self.driver.find_element(By.XPATH, "/html/body/div[1]/div[3]/div/div/div[2]/div[4]/div")
        lan = self.driver.find_elements(By.XPATH, "/html/body/div[1]/div/div/div[2]/div[1]/div/div/div[2]/div[1]/div[2]/a")
        date_list = self.driver.find_elements(By.XPATH, "/html/body/div[1]/div/div/div[2]/div[1]/div/div/div[2]/div[1]/div[2]/a/div[3]")
        for item in range(0, len(lan)):
            time.sleep(3)
            # if item.find_element(By.XPATH, "div[1]/div[2]").text == '公告标题':
            #     continue
            date = date_list[item].text
            lan[item].click()
            handles = self.driver.window_handles
            self.driver.switch_to.window(handles[-1])
            time.sleep(1.5)
            title = self.driver.find_element(By.CLASS_NAME, "f-detail-title").get_attribute("innerText")
            box = self.driver.find_element(By.CLASS_NAME, "f-detail-content")
            view_time = ''
            content = box.get_attribute("outerHTML")
            # attachments_1 = box.find_elements(By.TAG_NAME, "a")
            attachments_div = self.driver.find_elements(By.CLASS_NAME, "f-detail-attachment-wrap")
            attachments_list = []
            if len(attachments_div) > 0:
                attachments = attachments_div[0].find_elements(By.TAG_NAME, "a")
                attachment_name = attachments_div[0].find_elements(By.TAG_NAME, "span")
                if len(attachments) > 0:
                    for a in range(0, len(attachments)):
                        if attachments[a].text == '' or attachments[a].get_attribute("href") is None:
                            continue
                        elif "attachement" in attachments[a].get_attribute("href") or "附件" in attachments[a].text:
                            self.driver.execute_script("window.scroll(0,500)")
                            attachments[a].click()
                            attachments_list.append(attachment_name[a].get_attribute("innerText"))
                            time.sleep(1)
                            # href = a.get_attribute("href").split('.')[0]
                    time.sleep(1)
            while self.driver.current_url != 'https://www.gdmede.com.cn/':
                self.driver.close()
                handles = self.driver.window_handles
                self.driver.switch_to.window(handles[-1])
                time.sleep(1)
            data_list = [title, date, view_time, content, attachments_list]
            print(data_list)

    def get_last_filename_and_rename(self, sav_folder, new_filename):
        files = glob.glob(sav_folder + '/*')
        max_file = max(files, key=os.path.getctime)
        filename = max_file.split("/")[-1].split(".")[0]
        new_path = max_file.replace(filename, new_filename)
        os.rename(max_file, new_path)
        return new_path

    def close(self):
        time.sleep(60)
        self.driver.quit()

sd = SD()
sd.get("")
sd.close()

